<!--  IE需要es6-promise -->
<script src="plugins/aliyunvod/es6-promise.min.js"></script>
<script src="plugins/aliyunvod/aliyun-oss-sdk-5.3.1.min.js"></script>
<script src="plugins/aliyunvod/aliyun-upload-sdk-1.5.0.min.js"></script>
<script type="text/javascript">
var uploader;
var uploadAuth;
var uploadAddress;
function basename(direct) {
    if (!direct) return '';
    direct = direct.replace(/\\/g,'/');

    if (direct.indexOf('/') !== -1) {
        var result = direct.split('/');
        var a = result.pop();
        while (a ==='') {
            a = result.pop();
        }
        return a || '';
    } else {
        return direct;
    }
}
function plugin_aliyunvod_files_change(obj)
{
	var title = $("#title").val();
	if(!title){
		title = obj.files[0].name;
		$("#title").val(title);
	}
	var url = get_plugin_url("aliyunvod","vodauthinfo",'title='+$.str.encode(title)+"&filename="+$.str.encode(obj.files[0].name));
	var desc = $("#note").val();
	if(desc && desc != 'undefined'){
		url += "&info="+$.str.encode(desc);
		desc = ',"Description":"'+desc+'"';
	}else{
		desc = '';
	}
	var tag = $("#tag").val();
	if(tag && tag != 'undefined'){
		url += "&tag="+$.str.encode(tag);
		tag = ',"Tags":"'+tag+'"';
	}else{
		tag = '';
	}
	var userData = '{"Vod":{"Title":"'+title+'"'+desc+''+tag+'}}';
	uploader.addFile(obj.files[0], null, null, null, userData);
	var thumb = $("#thumb").val();
	if(thumb && thumb != 'undefined'){
		url += "&thumb="+$.str.encode(thumb);
	}
	$.phpok.json(url,function(rs){
		if(!rs.status){
			$.dialog.alert(rs.info);
			return false;
		}
		uploadAuth = rs.info.UploadAuth;
		uploadAddress = rs.info.UploadAddress;
		$("#videoid").val(rs.info.VideoId);
		uploader.startUpload();
	});
	//$("#show_files_progress").show().html('请点击按钮开始上传');
}

function plugin_aliyunvod_upload()
{
	var file = $("#files").val();
	if(!file){
		$.dialog.alert('请选择要上传的文件');
		return false;
	}
}
function plugin_aliyunvod_time_thumb(auto)
{
	var videoId = $("#videoid").val();
	var url = get_plugin_url('aliyunvod','videotime','videoid='+videoId);
	$.phpok.json(url,function(rs){
		if(!rs.status){
			return false;
		}
		$("#longtime").val(rs.info.time);
		$("#thumb").val(rs.info.thumb);
		if(auto && auto != 'undefined'){
			$("#show_files_progress").html('请提交数据进行保存');
		}
	});
}
function plugin_aliyunvod_filelist()
{
	var w = $("body").width();
	if(w>1366){
		w = '60%';
	}else{
		w = '700px';
	}
	var h = $(window).height();
	if(h>768){
		h = '70%';
	}else{
		h = '500px';
	}
	var url = get_plugin_url('aliyunvod','videolist');
	$.dialog.open(url,{
		'title':'请选择视频',
		'lock':true,
		'width':w,
		'height':h
	})
}
window.onload = new function(){
	uploader = new AliyunUpload.Vod({
		userId:"{$plugin.aliyunvod.param.aliyun_accout}",
		regoin:"{$plugin.aliyunvod.param.regoin_id}",
		// 文件上传失败
		'onUploadFailed': function (uploadInfo, code, message) {
			$.dialog.alert('上传文件：'+uploadInfo.file.name+' 失败<br/>错误代码：'+code+"<br/>提示内容："+message);
			$("#files").val('');
		},
		// 文件上传完成
		'onUploadSucceed': function (uploadInfo) {
			$("#show_files_progress").html('视频上传上成功，请提交数据进行保存');
			$("#files").val('');
		},
		// 文件上传进度
		'onUploadProgress': function (uploadInfo, totalSize, uploadedSize) {
			var size = 0;
			totalSize = parseInt(totalSize);
			if(totalSize>= 1073741824){
				size = ((totalSize/1073741824).toFixed(3)).toString() + 'GB';
			}else if(totalSize < 1073741824 && totalSize >= 1048576){
				size = ((totalSize/1048576).toFixed(3)).toString() + 'MB';
			}else if(totalSize < 1048576 && totalSize >= 1024){
				size = ((totalSize/1024).toFixed(3)).toString() + 'KB';
			}else{
				size = (totalSize).toString() + 'B';
			}
			$("#show_files_progress").show().html('正在上传：'+uploadInfo.file.name+'，文件大小：'+size+'，已上传：'+ Math.ceil(uploadedSize * 100 / totalSize)+'%');
		},
		// 开始上传
		'onUploadstarted': function (uploadInfo) {
			var vid = $("#videoid").val();
			uploader.setUploadAuthAndAddress(uploadInfo, uploadAuth, uploadAddress,vid);
			$("#show_files_progress").show().html('正在开始上传文件：'+uploadInfo.file.name+'，请稍候…');
		}
	});
	uploader.init();
}
$(document).ready(function(){
    //将标签移到上面
	//var html = $(".main1 div.table").last().html();
	//$(".main1 div.table").last().remove();
	//$(".main1 div.table").first().after('<div class="table">'+html+"</div>");
	var html = '<div class="layui-form-item">';
	html += '<label class="layui-form-label">视频上传</label>';
	html += '<div class="layui-input-block">';
	html += '<input type="file" name="file" id="files" onchange="plugin_aliyunvod_files_change(this)" class="layui-btn layui-btn-sm layui-btn-primary"/>';
	//html += '<input type="button" value="视频上传" onclick="plugin_aliyunvod_upload()" class="layui-btn layui-btn-sm layui-btn-danger" />';
	html += '&nbsp;<div class="layui-btn-group">'
	html += '<input type="button" value="选择已上传的视频" onclick="plugin_aliyunvod_filelist()" class="layui-btn layui-btn-sm layui-btn-normal" />';
	html += '<input type="button" value="获取时间及缩略图" onclick="plugin_aliyunvod_time_thumb()" class="layui-btn layui-btn-sm layui-btn-normal" />';
	html += '</div></div>';
	html += '<div class="layui-input-block mtop hide" style="color:darkblue;" id="show_files_progress"></div>';
	//html += '<div class="layui-input-block mtop">请选择要上传的文件，选择后，记得点击【视频上传】上传，如果视频已经存在，请不要重复上传，刚上传好的视频获取时间及缩略图可能会延迟，请耐心等待</div>';
	html += '</div>';
	$("#videoid").parent().parent().before(html);
	//$("#videoid").after('&nbsp;<input type="button" value="选择视频" onclick="plugin_aliyunvod_filelist()" class="layui-btn layui-btn-sm" />')
	//$("#longtime").after('&nbsp;<input type="button" value="获取时间" class="layui-btn layui-btn-sm" onclick="plugin_aliyunvod_time_thumb()" />')
});
</script>